﻿using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Home.Mobilesales.Common;
using Home.MobileSales.Common.Components;
using Home.MobileSales.Common.Entities;
using Home.MobileSales.Sales.Interface;
using Home.MobileSales.Common.Models;


namespace Home.MobileSales.Sales.Presenter
{
    public class SheduleVisitsPresenter
    {
        private ISheduleVisitsView _view;
        private SheduleVisitsRepository _dbGV;
        private BindingSource bindingSource;

        public SheduleVisitsPresenter(ISheduleVisitsView view)
        {
            try
            {
                _dbGV = DataManager.GetInstance().SheduleVisits;
                bindingSource = new System.Windows.Forms.BindingSource(_dbGV.Visit, "SheduleVisits");
                _view = view;
                _view.SetBindingSource(bindingSource);



                view.EditRecord += EditRecord;
                view.DeleteRecord += DeleteRecord;
                view.SaveChanges += SaveChanges;
                Filter[] f = new Filter[]
                                 {
                                     new Filter("Всі", "*"),
                                     new Filter("Понеділок", "Monday"),
                                     new Filter("Вівторок", "Tuesday"),
                                     new Filter("Середа", "Wednesday"),
                                     new Filter("Четвер", "Thursday"),
                                     new Filter("Пятниця", "Friday"),
                                     new Filter("Субота", "Saturday"),
                                     new Filter("Неділя", "Sunday"),
                                 };
                _view.SetSelectFilter(f);
                _view.SetFilter += SetFilter;

                _view.AddRecord += AddRecord;
            }
            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }

        }

        public void AddRecord(object sender, EventArgs e)
        {
            try
            {
                AddVisitDialog addVisitDialog = new AddVisitDialog(true);
                addVisitDialog.OK += AddVisit;
                addVisitDialog.Cancel += CancelAddVisitDialog;
                _view.ShowAddVisitDialog(addVisitDialog);
            }
            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }
        }

        public void CancelAddVisitDialog(object sender, EventArgs e)
        {
            _view.CloseAddDialog();
        }

        public void AddVisit(object sender, EventArgs e)
        {
            try
            {
                AddVisitDialog addVisitDialog = (AddVisitDialog)sender;
                DataRowView dr = (DataRowView)bindingSource.AddNew();
                BuyPoint buyPoint = ((BuyPoint)addVisitDialog.SelectedBuyPoint);
                dr["BuyPointId"] = buyPoint.IdBuyPoint;
                dr["NameBuyPoint"] = buyPoint.NameBuyPoint;
                dr["Adress"] = buyPoint.Adress;
                Dictionary<string, bool> day = addVisitDialog.SelectDays;
                dr["Sunday"] = day["Sunday"];
                dr["Monday"] = day["Monday"];
                dr["Tuesday"] = day["Tuesday"];
                dr["Wednesday"] = day["Wednesday"];
                dr["Thursday"] = day["Thursday"];
                dr["Friday"] = day["Friday"];
                dr["Saturday"] = day["Saturday"];

                _view.IsChange = true;
            }
            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }
        }

        public void EditRecord(object sender, EventArgs e)
        {
            try
            {
                string col = _view.ActiveColumn;
                DataRowView drv = (DataRowView)bindingSource.Current;
                bool val = !(drv[col] != DBNull.Value && drv[col].ToString() == "True");
                drv[col] = val;
                bindingSource.EndEdit();
                _view.IsChange = true;
            }
            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }
        }

        public void SetFilter(object sender, EventArgs e)
        {
            Filter filter = _view.FilterValue;
            if (filter.Value == "*")
            {
                bindingSource.RemoveFilter();
            }
            else
                bindingSource.Filter = filter.Value + " = 1";
        }

        public void DeleteRecord(object sender, EventArgs e)
        {
            try
            {
                bindingSource.RemoveCurrent();
                _view.IsChange = true;
            }
            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }
        }

        public void SaveChanges(object sender, EventArgs e)
        {
            try
            {
                bindingSource.EndEdit();
                _dbGV.SaveChanges();
            }
            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }
        }
    }
}
